package cn.lfzj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.lfzj.dao.OrderDao;
import cn.lfzj.dao.UserDao;
import cn.lfzj.model.Order;

@Service
public class OrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private UserDao userDao;

	/**
	 * 订单查询(多条件)
	 */
	public List<Order> selAllOrder(String orderId, int userId, String data, String startSta, String endSta,
			String orderState) {
		return orderDao.selAllOrder(orderId, userId, data, startSta, endSta, orderState);
	}

	/**
	 * 用户下单
	 */
	public int addOrder(Order order) {
		order.setOrderState("未支付");
		return orderDao.addOrder(order);
	}

	/**
	 * 支付成功修改订单状态
	 */
	@Transactional
	public int updateState(double amount, String orderId, int userId) {
		int i = 1;
		if (userDao.selMoney(userId) < amount) {
			orderDao.updateState("未支付", amount, orderId);
			i = 0;
		} else {
			userDao.updateMoney(amount, userId);
//			i = 1 / 0; // 手动制造异常
			orderDao.updateState("已支付", amount, orderId);
		}
		return i;
	}

	/**
	 * 用户端查询订单
	 */
	public List<Order> selByUserId(int userId) {
		return orderDao.selByUserId(userId);
	}

	/**
	 * 删除订单
	 */
	public int delOrder(String orderId) {
		return orderDao.delOrder(orderId);
	}
}
